package com.shipwe.loans.ui.fragment

import android.view.LayoutInflater
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.shipwe.loans.R
import com.shipwe.loans.databinding.FragmentOrderRepayBinding
import com.shipwe.loans.pro.ShipweFragment
import com.shipwe.loans.ui.activity.ActivityMain
import com.shipwe.loans.ui.activity.ActivityOrderRepay
import com.shipwe.loans.ui.adapter.OrderRepayAdapter
import com.shipwe.loans.ui.presenter.OrderRepayListPresenter
import com.shipwe.loans.ui.view.IOrderRepayListView
import com.shipwe.loans.utils.event.AppEventData
import com.shipwe.loans.utils.event.AppEventPointType
import org.json.JSONObject

/**
 * Shipwe
 * @Author wwdeng
 * @CreateTime: 2024/11/26 16:52
 * @Annotation：待还款
 */
class FragmentOrderRepay: ShipweFragment(),IOrderRepayListView {
    lateinit var binding: FragmentOrderRepayBinding
    private var presenter: OrderRepayListPresenter? = null
    //订单列表
    private var orderAdapter: OrderRepayAdapter? = null

    override fun getShipweRootView(inflater: LayoutInflater): View {
        binding = FragmentOrderRepayBinding.inflate(inflater)
        return binding.root
    }

    override fun initShipweView() {
        rootViewBinding.rootLayout.setBackgroundResource(R.drawable.page_login_bg)
        setShipweTitleBarVisibility(View.VISIBLE)
        setShipweBackBtnRes(R.drawable.menu_black)
        setShipweRightBtnRes(R.drawable.customer_black)
        setShipweTextTitleVisibility(View.GONE)
        initOrderRepayRefreshView()
        initOrderRepayListView()

    }

    override fun initShipweListener() {
        //菜单
        rootViewBinding.rootBack.setOnClickListener {
            (getAct() as ActivityMain).showMainMenuFragment("Repay Page")
        }
        //客服
        rootViewBinding.rootCustomer.setOnClickListener {
            showCustomerDialog("Repay Page")
            appEventPointRequestAction(AppEventPointType.CLICK.SHIPWE_CLICK_CUSTOMER,"Repay Page")
        }
    }

    override fun initShipweData() {
        presenter = OrderRepayListPresenter(this)
        presenter?.loadOrderRepayListAction()
    }

    /* 初始化刷新 */
    private fun initOrderRepayRefreshView() {
        //禁止加载
        binding.orderRepayRefreshView.setEnableLoadMore(false)
        binding.orderRepayRefreshView.setEnableAutoLoadMore(false)
        binding.orderRepayRefreshView.setOnRefreshListener {
            //刷新产品列表
            getAct().publishEventData(AppEventData(AppEventData.TYPE.SHIPWE_PRODUCT_REFRESH))
        }
        binding.orderRepayRefreshView.setOnLoadMoreListener {
            binding.orderRepayRefreshView.finishLoadMore()
        }
    }

    /**
     * 产品列表
     */
    private fun initOrderRepayListView(){
//        binding.orderRepayListView.setHasFixedSize(true)
//        binding.orderRepayListView.isNestedScrollingEnabled = false
//        binding.orderRepayListView.isFocusableInTouchMode = false //不需要焦点
        val layoutManager = LinearLayoutManager(context)
        binding.orderRepayListView.layoutManager = layoutManager
        orderAdapter = OrderRepayAdapter(binding.orderRepayListView)
        orderAdapter?.setOnItemChildClickListener { _, childView, position ->
            val item = this.orderAdapter!!.getItem(position)
            presenter?.setOrderRepaySelectOrder(item)
            when(childView.id){
                R.id.orderRepayDetails -> {//待还款详情
                    val pName = item.optString("product")
                    val pToken = item.optString("token")
                    val pTitle = item.optString("name")
                    ActivityOrderRepay.start(requireContext(), pTitle,pName, pToken)
                }
                R.id.repayEmptyText -> {//去申请
                    publishEventData(AppEventData(AppEventData.TYPE.SHIPWE_SHOW_HOME))
                }
            }
        }
        binding.orderRepayListView.adapter = orderAdapter
    }

    /**
     * 设置订单列表
     */
    override fun showOrderRepayList(orderList: ArrayList<JSONObject>) {
        orderAdapter?.data = orderList
    }

    override fun onShipwePublishEvent(event: AppEventData) {
        super.onShipwePublishEvent(event)
        when(event.eventType){
            AppEventData.TYPE.SHIPWE_PRODUCT_REFRESHED -> {//刷新产品列表完成
                presenter?.loadOrderRepayListAction()
            }
            AppEventData.TYPE.SHIPWE_PRODUCT_FINISHED -> {
                dismissShipweLoading()
            }
            else -> {

            }
        }
    }

    override fun dismissShipweLoading() {
        super.dismissShipweLoading()
        binding.orderRepayRefreshView.finishRefresh()
    }

    override fun onStart() {
        super.onStart()
        appEventPointRequestAction(AppEventPointType.PAGE.SHIPWE_PAGE_LOAN_REPAY_START)
    }

    override fun onDestroy() {
        super.onDestroy()
        appEventPointRequestAction(AppEventPointType.PAGE.SHIPWE_PAGE_LOAN_REPAY_END)
    }
}